package com.leyu.mall.site.shopcar.car;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.leyu.mall.domain.goods.GoodsBase;


/**
 * Filename :  ���ﳵ����һ��Map���ϣ����ڴ�Ź�����Ʒ�� �ṩ�Ƴ���Ʒ����չ��ﳵ������������Ʒ�������Լ��ܽ�
 */
public class ShopCar {
	// ��Ʒ����
	private Map<Long, GoodsBase> cars = new HashMap<Long, GoodsBase>();

	// �����鼮�����ﳵ
	public void addGoods(GoodsBase scb) {
		System.out.println("addGoods   " + scb);
		// �ж��Ƿ��е�ǰ���ڵ���Ʒ��������������������ɡ�
		if (cars.containsKey(scb.getGoodsId())) {
			// ȡ��ԭ�е�ͼ������
			int amount = ((GoodsBase) cars.get(scb.getGoodsId()))
					.getGoodsAmount();
			amount += scb.getGoodsAmount(); // ��������
			updateGoodsAmount(scb.getGoodsId(), amount);
		} else {
			// ���빺�ﳵ
			cars.put(scb.getGoodsId(), scb);
		}
	}

	// �Ƴ��ﳵ����Ʒ
	public void removeGoods(Long bookId) {
		if (cars.containsKey(bookId))
			cars.remove(bookId);
	}

	// ��չ��ﳵ
	public void clear() {
		cars.clear();
	}

	// �޸���Ӧ����Ʒ����
	public void updateGoodsAmount(Long goodsid, int amount) {
		// ���裬ȡ��cars����Ʒ���󡣸��¶���Ȼ���ٴ�put��ȥ
		GoodsBase scb = (GoodsBase) cars.get(goodsid);
		scb.setGoodsAmount(amount);
		cars.put(goodsid, scb);
	}
	
	//�@ȡĳ��Ʒ����
	public int getAmountBYID(Long goodsid){
		GoodsBase scb = (GoodsBase) cars.get(goodsid);
		return scb.getGoodsAmount();
	}

	// ��ȡ���ﳵ
	public Map getCars() {
		return cars;
	}

	// ���㹺�ﳵ��������Ʒ����
	public int getGoodsAllAmount() {
		int account = 0; // ����
		Iterator iter = cars.values().iterator();
		while (iter.hasNext()) {
			GoodsBase scb = (GoodsBase) iter.next();
			account += scb.getGoodsAmount();
		}
		return account;
	}

	// ���㹺�ﳵ��Ʒ�ܽ��
	public BigDecimal getGoodsAllPrice() {
		BigDecimal account = new BigDecimal("0"); // �ܽ��
		Iterator iter = cars.values().iterator();
		while (iter.hasNext()) {
			GoodsBase scb = (GoodsBase) iter.next();
			BigDecimal goodsPrice = new BigDecimal(String.valueOf(scb
					.getShopPrice()));
			BigDecimal goodsAmount = new BigDecimal(String.valueOf(scb
					.getGoodsAmount()));
			System.out.println("+++++++++++++++++"+goodsAmount);
			System.out.println("+++++++++++++++++"+goodsPrice);
			account = account.add(goodsPrice.multiply(goodsAmount));
		}
		return account;
	}
}
